const carousel = require('../../services/carousel.js');
const commodity = require('../../services/commodity.js');
const category = require('../../services/category.js');
//获取应用实例
const app = getApp()
Page({
    data: {
        carousels: [],
        category: [],
        hs: [],
    },
    onLoad() {
        this.carousel()
        this.category()
    },

    category: function () {
        let that = this
        const data = {
            status: '0'
        }
        category.list(data, (res) => {
            if (res.code === 0) {
                that.setData({
                    category: res.data
                })
                that.getHs(res.data)
            }
        })
    },

    getHs: function (list) {
        let hs = []
        for (let i in list) {
            const obj = {}
            obj['id'] = list[i].id
            obj['name'] = list[i].name
            hs.push(obj)
        }
        this.getHsList(hs)
    },

    getHsList: function (hs) {
        for (let i in hs) {
            const d = {
                cid: hs[i].id,
                hs: '0'
            }
            commodity.hs(d, (res) => {
                if (res.code === 0) {
                    hs[i].list = res.data
                    this.setData({
                        hs: hs
                    })
                }
            })

        }

    },

    /**
     * 轮播图接口
     */
    carousel: function () {
        let that = this
        carousel.carousel(null, (res) => {
            if (res.code == 0) {
                that.setData({
                    carousels: res.data
                })
            }
        })
    },

    /**
     * 跳转到详情页
     */
    toDetail: function (e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../detail/detail?id=' + id
        })
    },

    /**
     * 跳转到搜索页面
     */
    toSearch: function () {
        wx.navigateTo({
            url: '../search/search'
        })
    },

    toList: function (e) {
        const id = e.currentTarget.dataset.id
        const name = e.currentTarget.dataset.name
        wx.navigateTo({
            url: '../list/list?id=' + id + '&name=' + name
        })
    }
})
